package logic

import (
	"campus-trade/models/mysql"
	"context"

	"campus-trade/campusrpc/campusrpc"
	"campus-trade/campusrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PingLunListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPingLunListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLunListLogic {
	return &PingLunListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PingLunListLogic) PingLunList(in *campusrpc.PingLunListRequest) (*campusrpc.PingLunListResponse, error) {
	// todo: add your logic here and delete this line

	var cd []mysql.BookPingLun
	mysql.DB.Debug().Model(&mysql.PingLun{}).
		Select("pingLuns.*,books.id as book_id").
		Joins("left join pingLuns on pingLuns.id = pingLuns.book_id").
		Find(&cd)

	var cache []*campusrpc.PingLunList

	for _, i2 := range cd {
		list := campusrpc.PingLunList{
			BookId: i2.BookId,
			Name:   i2.Name,
		}
		cache = append(cache, &list)
	}
	return &campusrpc.PingLunListResponse{
		List: cache,
	}, nil
}
